Very basic beginner Ruby question to do with elsif and ranges [migrated]
Posted
by
MattKneale
on Programmers
See other posts from Programmers
or by MattKneale
Published on 2014-05-27T15:09:36Z
Indexed on
2014/05/27
15:57 UTC
Read the original article
Hit count: 202
ruby
I've been trying to get to grasps with Ruby (for all of an hour) and this is my first language.
I've got the following code:
var_comparison = 5
print "Please enter a number: "
my_num = Integer(gets.chomp)
if my_num > var_comparison
print "You picked a number greater than 5!"
elsif my_num < var_comparison
print "You picked a number less than 5!"
elsif my_num > 99
print "Your number is too large, man."
else
print "You picked the number 5!"
end
Clearly the interpreter has no way of distinguishing between accepting the rule >5 or >99. How do I make it so that any number between 6-99 returns "You picked a number greater than 5!", but a number 100 or greater returns "Your number is too large, man!"?
Do I need to specifically state a range somehow? How would I best do that? Would it by the normal range methods e.g.
if my_num 6..99
or
if my_num.between(6..99)
?
© Programmers or respective owner